package com.ccb.sc.luxury.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.luxury.query.AppLuxurySearchParam;
import com.ccb.sc.luxury.service.AppLuxuryService;
import com.ccb.sc.luxury.vo.*;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.common.service.IDeliveryService;
import com.ccb.sc.common.modules.common.vo.QueryTrackResp;
import com.ccb.sc.common.modules.luxury.entity.LuxuryLogistics;
import com.ccb.sc.common.modules.luxury.entity.LuxuryOrder;
import com.ccb.sc.common.modules.luxury.service.LuxuryLogisticsService;
import com.ccb.sc.common.modules.setting.service.ISettingsService;
import com.ccb.sc.common.modules.setting.vo.LuxurySettingsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ken
 * @version 1.0
 * @date 2025/2/23 15:04
 */
@RestController
@RequestMapping("/app/luxury")
@Api(value = "奢侈品管理", tags = "奢侈品管理")
public class LuxuryController {

    @Autowired
    private AppLuxuryService luxuryService;

    @Autowired
    private LuxuryLogisticsService logisticsService;

    @Autowired
    private IDeliveryService deliveryService;

    @Autowired
    private ISettingsService settingsService;

    @TokenAuth
    @GetMapping("/list")
    @ApiOperation(value = "奢侈品列表", response = LuxuryListVo.class, responseContainer = "list")
    public ResBody<?> list(@CurrentUser @ApiIgnore TokenModel token, AppLuxurySearchParam queryParam) {
        PageVo<LuxuryListVo> list = this.luxuryService.list(token.getId(), queryParam);
        return ResBody.success(list);
    }

    @TokenAuth
    @GetMapping("/detail/{orderId}")
    @ApiOperation(value = "奢侈品详情", response = LuxuryDetailVo.class, responseContainer = "list")
    public ResBody<?> detail(@CurrentUser @ApiIgnore TokenModel token, @PathVariable String orderId) {
        LuxuryDetailVo detailVo = this.luxuryService.detail(token.getId(), orderId);
        return ResBody.success(detailVo);
    }

    @TokenAuth
    @PostMapping("/apply")
    @ApiOperation(value ="回收申请")
    public ResBody<?> apply(@ApiIgnore @CurrentUser TokenModel token, @RequestBody @Validated LuxuryApplyVo applyVo) {
        this.luxuryService.apply(token.getId(), applyVo);
        return ResBody.success();
    }

    @TokenAuth
    @PostMapping("/post")
    @ApiOperation(value ="邮寄")
    public ResBody<?> post(@ApiIgnore @CurrentUser TokenModel token, @RequestBody @Validated LuxuryPostVo postVo) {
        this.luxuryService.post(token.getId(), postVo);
        return ResBody.success();
    }

    @TokenAuth
    @PostMapping("/accept")
    @ApiOperation(value ="接受定价")
    public ResBody<?> accept(@ApiIgnore @CurrentUser TokenModel token, @RequestBody @Validated LuxuryAcceptVo acceptVo) {
        this.luxuryService.accept(token.getId(), acceptVo);
        return ResBody.success();
    }

    @TokenAuth
    @PostMapping("/refuse")
    @ApiOperation(value ="拒绝定价")
    public ResBody<?> refuse(@ApiIgnore @CurrentUser TokenModel token, @RequestBody @Validated LuxuryRefuseVo refuseVo) {
        this.luxuryService.refuse(token.getId(), refuseVo);
        return ResBody.success();
    }

    @GetMapping("/setting")
    @ApiOperation(value = "获取奢侈品设置", response = LuxurySettingsVo.class)
    public ResBody<?> setting(){
        LuxurySettingsVo settings = this.settingsService.getSettings(LuxurySettingsVo.class);
        return ResBody.success(settings);
    }

    @TokenAuth
    @GetMapping("/trace/{logisticsId}")
    @ApiOperation(value = "订单物流信息", response = QueryTrackResp.class)
    public ResBody<?> trace(@CurrentUser @ApiIgnore TokenModel token, @PathVariable Long logisticsId) {
        LuxuryLogistics logistics = this.logisticsService.getById(logisticsId);
        if (logistics == null) {
            return ResBody.success();
        }
        LuxuryOrder luxuryOrder = this.luxuryService.getById(logistics.getOrderId());
        Assert.isTrue(token.getId().equals(luxuryOrder.getMemberId()), "订单不存在");
        if (StringUtils.hasText(logistics.getLogisticsNo())) {
            QueryTrackResp queryTrackResp = this.deliveryService.queryTrack(logistics.getLogisticsCompany(), logistics.getLogisticsNo(), logistics.getPhone());
            return ResBody.success(queryTrackResp);
        }
        return ResBody.success();
    }
}
